// 将图片作为材质添加到cesium中，供绘制流动的线段使用
// import * as Cesium from 'cesium'

(function () {
    console.log('调用了流光线')

    // 流动线纹理
    function PolylineTrailLinkMaterialProperty({color, duration}) {
        console.log('调用了流光线带参数函数')
        this._definitionChanged = new Cesium.Event()
        this._color = undefined
        this._colorSubscription = undefined
        this.color = color
        this.duration = duration
        this._time = new Date().getTime()
    }

    Object.defineProperties(PolylineTrailLinkMaterialProperty.prototype, {
        isConstant: {
            get: function () {
                return false
            },
        },
        definitionChanged: {
            get: function () {
                return this._definitionChanged
            },
        },
        color: Cesium.createPropertyDescriptor('color'),
    })

    PolylineTrailLinkMaterialProperty.prototype.getType = function (time) {
        return 'PolylineTrailLink'
    }
    PolylineTrailLinkMaterialProperty.prototype.getValue = function (
        time,
        result
    ) {
        if (!Cesium.defined(result)) {
            result = {}
        }
        result.color = Cesium.Property.getValueOrClonedDefault(
            this._color,
            time,
            Cesium.Color.WHITE,
            result.color
        )
        result.image = Cesium.Material.PolylineTrailLinkImage
        result.time =
            ((new Date().getTime() - this._time) % this.duration) /
            this.duration
        return result
    }
    PolylineTrailLinkMaterialProperty.prototype.equals = function (other) {
        return (
            this === other ||
            (other instanceof PolylineTrailLinkMaterialProperty &&
                Cesium.Property.equals(this._color, other._color))
        )
    }

    Cesium.PolylineTrailLinkMaterialProperty = PolylineTrailLinkMaterialProperty
    Cesium.Material.PolylineTrailLinkType = 'PolylineTrailLink'
    Cesium.Material.PolylineTrailLinkImage = '' //图片 图片为箭头
    Cesium.Material.PolylineTrailLinkSource =
        `czm_material czm_getMaterial(czm_materialInput materialInput)
          {
              czm_material material = czm_getDefaultMaterial(materialInput);
              vec2 st = materialInput.st;
              vec4 colorImage = texture2D(image, vec2(fract(st.s - time), st.t));
              material.alpha = colorImage.a;
              material.diffuse = (colorImage.rgb+color.rgb)/2.0;
              return material;
          }`

    Cesium.Material._materialCache.addMaterial(
        Cesium.Material.PolylineTrailLinkType,
        {
            fabric: {
                type: Cesium.Material.PolylineTrailLinkType,
                uniforms: {
                    color: new Cesium.Color(255.0, 255.0, 255.0, 1),
                    image: Cesium.Material.PolylineTrailLinkImage,
                    time: 0,
                },
                source: Cesium.Material.PolylineTrailLinkSource,
            },
            translucent: function (material) {
                return true
            },
        }
    )
})();

(function () {
    const defaultColor = Cesium.Color.WHITE;
    const defaultImageimageW = null
    const defaultAnimation = false
    const defaultDuration = 3000;

    function ImageLineMaterial(opt) {
        opt = Cesium.defaultValue(opt, Cesium.defaultValue.EMPTY_OBJECT);
        console.log('ImageLineMaterialProperty构造', opt)
        this._definitionChanged = new Cesium.Event();
        // 定义材质变量
        this._color = undefined;
        this._colorSubscription = undefined;
        this._backgroundColor = undefined;
        this._backgroundColorSubscription = undefined;
        this._image = undefined;
        this._imageSubscription = undefined;
        this._imageW = undefined;
        this._imageH = undefined
        this._imageWSubscription = undefined;
        this._animation = undefined;
        this._animationSubscription = undefined;
        this._duration = undefined;
        this._durationSubscription = undefined;
        // 变量初始化
        this.color = opt.color || defaultColor; //颜色
        this.backgroundColor = opt.backgroundColor || defaultColor; //颜色
        this._image = opt.image || null; //材质图片
        this.imageW = opt.imageW || defaultImageimageW
        this.imageH = opt.imageH
        this.animation = opt.animation || defaultAnimation
        this.duration = opt.duration || defaultDuration
        this._time = undefined;
    }
// 材质类型
    ImageLineMaterial.prototype.getType = function (time) {
        return "ImageLine";
    };
// 这个方法在每次渲染时被调用，result的参数会传入glsl中。
    ImageLineMaterial.prototype.getValue = function (time, result) {
        if (!Cesium.defined(result)) {
            result = {};
        }
        result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, defaultColor, result.color);
        result.backgroundColor = Cesium.Property.getValueOrClonedDefault(this._backgroundColor, time, defaultColor, result.backgroundColor);
        result.image = this._image;
        result.imageW = this._imageW;
        result.imageH = this._imageH;
        result.animation = this._animation;
        if (this._time === undefined) {
            this._time = new Date().getTime();
        }
        result.time = ((new Date().getTime() - this._time)%this._duration) / this._duration;
        return result;
    };

    ImageLineMaterial.prototype.equals = function (other) {
        return this === other ||
            other instanceof ImageLineMaterial && Cesium.Property.equals(this._color, other._color) && Cesium.Property.equals(this._backgroundColor, other._backgroundColor);
    };

    Object.defineProperties(ImageLineMaterial.prototype, {
        isConstant: {
            get: function get() {
                return false;
            }
        },
        definitionChanged: {
            get: function get() {
                return this._definitionChanged;
            }
        },
        color: Cesium.createPropertyDescriptor('color'),
        backgroundColor: Cesium.createPropertyDescriptor('backgroundColor'),
        image: Cesium.createPropertyDescriptor('image'),
        imageW: Cesium.createPropertyDescriptor('imageW'),
        imageH: Cesium.createPropertyDescriptor('imageH'),
        animation: Cesium.createPropertyDescriptor('animation'),
        duration: Cesium.createPropertyDescriptor('duration'),
    });
    Cesium.Material._materialCache.addMaterial("ImageLine", {
        fabric: {
            type: "ImageLine",
            uniforms: {
                // uniforms参数跟我们上面定义的参数以及getValue方法中返回的result对应，这里值是默认值
                color: new Cesium.Color(1, 0, 0, 1.0),
                backgroundColor: new Cesium.Color(0, 0, 0, 0.0),
                image: '',
                imageW: 10,
                imageH: 10,
                animation: false,
                duration: 1000,
                time: 0
            },
            // source编写glsl，可以使用uniforms参数，值来自getValue方法的result
            source: `
            #extension GL_OES_standard_derivatives : enable
            czm_material czm_getMaterial(czm_materialInput materialInput)
                {
                    czm_material material = czm_getDefaultMaterial(materialInput);
                    vec2 st = materialInput.st;
                    float s = st.s/ (abs(fwidth(st.s)) * imageW * czm_pixelRatio);
                    s = s-time;//增加运动效果
                    float t = st.t/ (abs(fwidth(st.t)) * imageH * czm_pixelRatio);
                    vec4 colorImage = texture2D(image, vec2(fract(s), st.t));
                    material.alpha = colorImage.a;
                    material.diffuse = colorImage.rgb;
                    return material;
                }
               `
        },
        translucent: function translucent() {
            return true;
        }
    });
// 写到Cesium对象上，就可以像其他MaterialProperty一样使用了
    Cesium.Material.ImageLineType = 'ImageLine'
    Cesium.ImageLineMaterialProperty = ImageLineMaterial
})();